package com.efuture.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description: 动态代理模式演示
 * @author zhaoshanshan
 * @date 2021/10/4 10:34 下午
 * @version 1.0
 */
public class TestProxy {

    public static void main(String[] args) {

        Lenovo lenovo = new Lenovo();
        SaleComputer proxyLenovo =  (SaleComputer)Proxy.newProxyInstance(lenovo.getClass().getClassLoader(), lenovo.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("invoke方法被执行了");
                if(method.getName().equals("sale")){
                    //增强sale方法 修改传入参数
                    double money = (double)args[0];
                    money = money*0.9;
                    return method.invoke(lenovo,money);
                }else{
                    return method.invoke(lenovo,args);
                }
            }
        });
        proxyLenovo.sale(8000);
        proxyLenovo.show();

    }
}
